package com.miyake.client.profile;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miyake.client.DataService;
import com.miyake.client.DataServiceAsync;
import com.miyake.client.DoctorsUserService;
import com.miyake.client.DoctorsUserServiceAsync;
import com.miyake.client.datamanage.DataManageMain;
import com.miyake.client.datamanage.DataViewer;
import com.miyake.client.datamanage.NodeProperty;
import com.miyake.client.datamanage.ToolBarHandler;
import com.miyake.client.tools.ClickableLabel;

public class OpenDataList extends Composite {
	private DataServiceAsync service = DataService.Util.getInstance();
	public OpenDataList(String userKey) {
		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setWidth("100%");
//		verticalPanel.setBorderWidth(1);
		VerticalPanel contentPanel = new VerticalPanel();
		verticalPanel.add(new HTML("<h2>Data List</h2>"));
		
		verticalPanel.add(contentPanel);
		getOpenDataList(userKey, contentPanel);
	}

	private void getOpenDataList(String userKey, final VerticalPanel verticalPanel) {
		final Grid projectGrid = new Grid();
		final Grid folderGrid = new Grid();
		final Grid paperGrid = new Grid();
		
		verticalPanel.add(new HTML("<b>Project</b>"));
		verticalPanel.add(projectGrid);
		verticalPanel.add(new HTML("<b>Folder</b>"));
		verticalPanel.add(folderGrid);
		verticalPanel.add(new HTML("<b>Paper</b>"));
		verticalPanel.add(paperGrid);	
		
		service.getOpenDataList(userKey, new AsyncCallback<List<NodeProperty>>() {
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<NodeProperty> result) {
				for (NodeProperty p : result) {
					if (p.getType().equals(NodeProperty.ROOTNODE)) {
						showGrid(projectGrid, p);
					}
					else if (p.getType().equals(NodeProperty.FOLDER)) {
						showGrid(folderGrid, p);
					}
					else if (p.getType().equals(NodeProperty.DATA)) {
						
					}
					else if (p.getType().equals(NodeProperty.IMAGE)) {
						
					}
					else if (p.getType().equals(NodeProperty.PLOT)) {
						
					}
					else if (p.getType().equals(NodeProperty.PAPER)) {
						showGrid(paperGrid, p);
					}
				}
			}
		
		});
	}

	protected void showGrid(Grid grid, final NodeProperty p) {
		grid.setBorderWidth(1);
		grid.resize(grid.getRowCount() + 1, 2);
		grid.setText(grid.getRowCount() - 1, 0, String.valueOf(grid.getRowCount()));
		final VerticalPanel panel = new VerticalPanel();
		final SimplePanel dataPanel = new SimplePanel();
		panel.add(new ClickableLabel(p.getName(), new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if (dataPanel.getWidget() == null) {
					DataManageMain	data = new DataManageMain(p.getKey());
					dataPanel.add(data);
				}
				else {
					dataPanel.clear();
				}
			}
		}));
		panel.add(dataPanel);
		grid.setWidget(grid.getRowCount() - 1, 1, panel);
	}
}
